<template>
  <div class="app-container">
    <div class="filter-container">
      <span style="float:left;width:20%;">
        <el-input @keyup.enter.native="handleFilter" v-model="listQuery.conKey" placeholder="请输入关键字" class="filter-item" />
      </span>
      <span style="float:left;margin-left: 10px;">
        <el-button @click="handleFilter" class="filter-item" type="primary" v-waves icon="el-icon-search">查询</el-button>
        <el-button @click="handleCreate" :disabled="btnDisabled" class="filter-item" type="primary" v-waves icon="el-icon-plus">添加
        </el-button>
        <el-button @click="handleExport" class="filter-item" :disabled="btnDisabled" type="warning" v-waves icon="el-icon-download">导出</el-button>
      </span>
    </div>
    <!-- 表格 -->
    <el-table :data="list" v-loading="listLoading" border fit highlight-current-row style="width: 100%;height: 100%;">
      <el-table-column width="60px" align="center" label="序号">
        <template slot-scope="scope">
          <span>{{ scope.$index +1 + (listQuery.pageSize*(listQuery.page-1))}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="conKey" sortable label="关键字"></el-table-column>
      <el-table-column align="center" prop="conValue" label="值"></el-table-column>
      <el-table-column align="center" prop="conDesc" label="描述"></el-table-column>
      <el-table-column align="center" prop="updateTime" label="操作时间" width="170px"></el-table-column>
      <el-table-column label="状态" width="80px" align="center">
        <template slot-scope="scope">
          <span>{{getNameByCode(scope.row.conStatus, statusOps)}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="handleUpdate(scope.row.id)" size="small" type="success" v-waves icon="el-icon-edit">修改</el-button>
          <el-button @click="handleDelete(scope.row.id)" size="small" type="danger" v-waves icon="el-icon-delete">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 添加修改弹出框页面组件 -->
    <add-update v-if="showAdd" @flushList="flushList" @closeDialog="closeDialog" :restPath="restPath" :showAdd="showAdd" :btnType="btnType" :rowId="rowId" :allDictData="allDictData" />

  </div>
</template>

<script>

  /* mixins */
  import pageListMixin from '@/mixins/pageList/pageListMixin.js'

  import {
  } from '@/api/sys/role'
  import { getDictData } from '@/api/common'

  /* 组件 */
  import addUpdate from './addUpdate.vue'

  /* 工具 */
  import * as common from '@/utils/base/common'
  import { getNameByCode } from '@/utils/dictData'
  import * as excel from '@/utils/export2Excel'

  export default {
    /* 常量管理 */
    name: 'constant',
    data() {
      return {
        restPath: 'sys/constant',
        listQuery: {
          page: 1,
          pageSize: 10,
          conKey: '',
        },

        allDictData: {
          statusOps: []
        },
      }
    },
    mixins: [pageListMixin],
    components: {
      'add-update': addUpdate,
    },
    methods: {
      initData() {
        this.getAllDictData()
        this.getList()
      },
      
      getAllDictData(){
        const params = {
          dictIds: "STATUS"
        }
        getDictData(params).then(response => {
          let data = response.data
          this.allDictData.statusOps = data.STATUS
        }).catch(err => {
          common.message(this, 'error', 2, '获取数据字典失败!')
          console.error(err)
        })
      },
      
      getNameByCode(val, dictMap){
        return getNameByCode(val, dictMap)
      },

      // 导出Excel
      handleExport() {
        let listAll = this.list
        if(listAll) {
          if(listAll.length > 0) {
            // 表头信息
            const tHeader = ['序号', '关键字', '值', '描述']
            // 表头对应字段名
            const filterVal = ['num', 'conKey', 'conValue', 'conDesc']
            // 数据
            const list = listAll
            for(var i = 0; i < list.length; i++) {
              list[i].num = (i + 1);
            }
            // 导出Excel文件名
            const excelNm = '常量'
            excel.export2ExcelToJs(tHeader, filterVal, list, excelNm, true)
          } else {
            common.message(this, 'warning', 2, '暂无数据!')
          }
        } else {
          common.message(this, 'warning', 2, '暂无数据!')
        }
      },

    }
  }
</script>

<style scoped>
  @import "~@/styles/list.css";
</style>